package com.finance.framework.lang.valid;

import com.alibaba.druid.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by ljg on 2017/4/7.
 */
public class EnumValidImpl  implements ConstraintValidator<EnumValid, String> {
    private String[]enums;
    @Override
    public void initialize(EnumValid enumValid) {
        this.enums=enumValid.enums();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtils.isEmpty(s)){
            return true;
        }
        if(enums==null||enums.length==0){
            return false;
        }
        for (String en:enums){
            if(s.equals(en)){
                return true;
            }
        }
        return false;
    }
}
